<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="标题" prop="processInstanceName">
        <el-input
          v-model="queryParams.processInstanceName"
          placeholder="请输入标题"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分组" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入分组"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程定义" prop="procDefId">
        <el-select v-model="queryParams.procDefId" size="small" clearable placeholder="请选择流程定义">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="处理人" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入处理人账号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processList">
      <!-- <el-table-column type="selection" width="55"  /> -->
      <el-table-column label="ID" width="100" prop="processInstanceId" />
      <el-table-column label="流程" prop="processDefinitionName" />
      <el-table-column label="标题" prop="processInstanceName" />
      <el-table-column label="发起时间" width="160" prop="startTime" />
      <el-table-column label="发起人" prop="startUserNickName" />
      <el-table-column width="300" label="状态">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.ended ? '#909399' : '#529cfd' }">
            {{ (scope.row.desc ? scope.row.desc + "，" : "") + scope.row.state }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="showHistory(scope.row)">审批历史</el-button>
          <el-button
            v-if="scope.row.userId !== 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-table v-loading="historyLoading" :data="historyList">
        <el-table-column label="ID" width="100" prop="id" />
        <el-table-column label="节点" prop="name" />
        <el-table-column label="处理人" prop="assigneeName" />
        <el-table-column label="开始时间" prop="startTime" />
        <el-table-column label="结束时间" prop="endTime" />
        <el-table-column label="处理方式" prop="action" />
        <el-table-column label="意见" prop="comment" />
      </el-table>

      <pagination
        v-show="historyTotal > 0"
        :total="historyTotal"
        :page.sync="historyParams.pageNum"
        :limit.sync="historyParams.pageSize"
        @pagination="getHistory"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listDefinition } from "@/api/activiti/definition.js";
import { listProcess, listHistory, removeProcInstance } from "@/api/activiti/example.js";

export default {
  name: "Modeler",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 审批历史表格数据
      historyList: [],
      // 查询审批历史参数
      historyParams: {
        pageNum: 1,
        pageSize: 10,
        processInstanceId: undefined,
      },
      // 审批历史总条数
      historyTotal: 0,
      // 遮罩层
      historyLoading: true,
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      //定义列表
      options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processInstanceName: undefined,
        category: undefined,
        procDefId: undefined,
        username: undefined,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        key: [{ required: true, message: "KEY不能为空", trigger: "blur" }],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getOptions();
    this.getList();
  },
  methods: {
    /** 查询流程定义列表 */
    getOptions() {
      listDefinition().then((response) => {
        this.options = response.rows;
      });
    },
    /** 查询模型列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then((response) => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询审批历史 */
    showHistory(data) {
      this.historyList = [];
      this.title = data.processDefinitionName;
      this.open = true;
      this.historyParams = {
        pageNum: 1,
        pageSize: 10,
        processInstanceId: data.processInstanceId,
      };
      this.getHistory();
    },
    getHistory() {
      this.historyLoading = true;
      listHistory(this.historyParams).then((response) => {
        this.historyList = response.rows;
        this.historyTotal = response.total;
        this.historyLoading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("是否确认删除流程“" + row.processInstanceName + "”?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return removeProcInstance(row.processInstanceId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
  },
};
</script>

<style scoped></style>
